﻿/// <reference path="../../../Scripts/jquery-1.8.2.js" />
/// <reference path="../../../Scripts/jQuery.mozcms.js" />
/// <reference path="../Kendo/kendo.web.min.js" />
(function (jQuery, undefined) {
    $(document).ready(function () {
        kendo.culture('zh-CN');
        $('select.kendoui').kendoDropDownList();
        autoBind();
    });

    function autoBind() {
        $('select.moz-auto-change').change(function () {
            hashgo('?' + $(this).attr('name') + '=' + $(this).val());
        });
    }
    var internal = window;

    internal.hashgo = function (url) {
        goto(url + location.hash);
    }

    internal.ajaxDelete = function (url, data, callback, cstr) {
        ///<summary>AJAX删除操作</summary>
        if (cstr && !confirm(cstr))
            return;
        $.ajax({
            url: url,
            dataType: "json",
            type: "DELETE",
            data: data,
            success: function (data) {
                if (data.error) {
                    showError(data.error);
                }
                else if (data.message) {
                    showSuccess(data.message);
                    if (callback) callback(data);
                }
            },
            error: function (ex) {
                showError(ex.responseText);
            }
        });
    };

    internal.ajaxUdSort = function (url, data, id, isDown, callback) {
        ///<summary>排序操作</summary>
        var postData = {
            id:function(){
                return id;
            },
            isDown: function () {
                return isDown;
            }
        };
        if (data)
            $.extend(postData, data);
        $.ajax({
            url: url,
            dataType: "json",
            type: "POST",
            data: postData,
            success: function (data) {
                if (data.error) {
                    showError(data.error);
                }
                else if (data.message) {
                    showSuccess(data.message);
                    if (callback) callback(data);
                }
            },
            error: function (ex) {
                showError(ex.responseText);
            }
        });
    }

    internal.showError = function(message) {
        show('error', message);
    }

    internal.showInfo = function(message) {
        show('info', message);
    }

    internal.showSuccess = function(message) {
        show('success', message);
    }

    function show(key, message) {
        $('div.nullnotewrapper, div.notewrapper').remove();
        $('<div class="notewrapper"><div class="k-' + key + '-colored note">' + message + '</div></div>').prependTo('div.main');
    }
    /*对kendoGrid进行封装，不需要重复写下列代码
    *window.grid=>data:source:refresh:self
    **************************************/
    jQuery.fn.extend({
        grid: function (options) {
            bindSource(options.transport, options.pageSize, options.schema);
            bindGrid(this, options.columns, options.toolbar, options.checkall, options.pageSize, options.gridin);
            internal.grid = {};
            jQuery.extend(internal.grid, {
                data: function () { return _grid.data("kendoGrid"); },
                source: function () { return _source; },
                refresh: function () {
                    _source.read();
                    _grid.data("kendoGrid").refresh();
                },
                self: function () { return _grid; }
            });
            return _grid;
        }
    });

    var _grid, _source;
    function bindSource(transport, pageSize, schema) {
        var options = {
            transport: transport,
            schema: schema || {
                total: function (d) {
                    return d.size;
                },
                data: function (d) {
                    return d.data;
                }
            }
        };
        if (pageSize && pageSize > 0)
            jQuery.extend(options, {
                batch: true,
                serverPaging: true,
                serverSorting: true,
                pageSize: pageSize
            });
        _source = new kendo.data.DataSource(options);
    }

    function bindGrid(selector, columns, toolbarSelector, checkboxName, pageable, gridin) {
        var options = {
            dataSource: _source,
            pageable: false,
            columns: columns,
            editable: 'inline',
            toolbar: toolbarSelector ? kendo.template($(toolbarSelector).html()) : null,
            scrollable: false,
            sortable: true
        };
        if (pageable && pageable > 0)
            jQuery.extend(options, {
                pageable: {
                    messages: {
                        first: res.pager.first,
                        last: res.pager.last,
                        previous: res.pager.previous,
                        next: res.pager.next,
                        display: res.pager.display,
                        empty: res.pager.empty,
                        itemsPerPage: res.pager.itemsPerPage
                    }
                }
            });
        if (gridin)
            jQuery.extend(true, options, gridin);
        _grid = selector.kendoGrid(options);
        if (checkboxName && columns[0].title == "chkall")
            _grid.find('th[data-title=chkall]').html('<input type="checkbox" onclick="$(\'input[name=' + checkboxName + ']\').checked(this.checked);"/>');
        autoBind();
    }
})(jQuery);